/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * 继承ScrollView用来放开滚动监听接口
 * @author flan
 * @date   2015年10月29日
 */
public class ObservableScrollView extends ScrollView{
	
	public int scrollDownY = 0;
	public float downY = 0;
	
	private OnScrollViewListener onScrollViewListener = null;  

	public ObservableScrollView(Context context) {
		super(context);
	}

	public ObservableScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public ObservableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent event) {
		
		//记录scrollview滚动前，手指按在屏幕上时的Y坐标
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			downY = event.getY();
			scrollDownY = this.getScrollY();
		}
		
		return super.onInterceptTouchEvent(event);
	}
	

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if (onScrollViewListener != null) {  
			onScrollViewListener.onScrollChanged(this, l, t, oldl, oldt);  
        }
	}
	
	public void setOnScrollViewListener(OnScrollViewListener onScrollViewListener) {
		this.onScrollViewListener = onScrollViewListener;
	}



	public static interface OnScrollViewListener {  
	    void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);  
	}  
}
